package com.cndw.kungfu.comm.tencent;

import java.util.Map;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.MD5;
import com.cndw.kungfu.ext.TimeProcesser;

public class LoginObjQQ implements LoginObj {

	private static final String objSpilt = ",";
	/**
	 * 玩家帐号
	 */
	private String openId;
	/**
	 * 登录签名
	 */
	private String openKey;
	/**
	 * 來源
	 */
	private String pf;
	/**
	 * 來源前面
	 */
	private String pfKey;
	/**
	 * 登录时间
	 */
	private int time;
	/**
	 * 签名
	 */
	private String sign;
	/**
	 * 是否防沉迷
	 */
	private boolean al;
	/**
	 * 是否年费黄钻
	 */
	private boolean yYear;
	/**
	 * 黄钻等级
	 */
	private int yLevel;
	/**
	 * 登录类型
	 */
	private int type;
	/**
	 * 登陆扩展
	 */
	private String ext;
	/**
	 * 3366等级
	 */
	private int lv3;
	
	private int appCustom;

	public LoginObjQQ(Map<String, Object> map) {
		parseMap(map);
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getOpenKey() {
		return openKey;
	}

	public void setOpenKey(String openKey) {
		this.openKey = openKey;
	}

	public String getPf() {
		return pf;
	}

	public void setPf(String pf) {
		this.pf = pf;
	}

	public String getPfKey() {
		return pfKey;
	}

	public void setPfKey(String pfKey) {
		this.pfKey = pfKey;
	}

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public boolean isAl() {
		return al;
	}

	public void setAl(boolean al) {
		this.al = al;
	}

	public boolean isyYear() {
		return yYear;
	}

	public void setyYear(boolean yYear) {
		this.yYear = yYear;
	}

	public int getyLevel() {
		return yLevel;
	}

	public void setyLevel(int yLevel) {
		this.yLevel = yLevel;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	private void parseMap(Map<String, Object> map) {
		if (map.containsKey(LoginKey.openId)) {
			openId = map.get(LoginKey.openId).toString();
		}

		if (map.containsKey(LoginKey.time)) {
			time = Integer.valueOf(map.get(LoginKey.time).toString());
		}

		if (map.containsKey(LoginKey.pf)) {
			pf = map.get(LoginKey.pf).toString();
		}
		if (map.containsKey(LoginKey.sign)) {
			sign = map.get(LoginKey.sign).toString();
		}		
		if (map.containsKey(LoginKey.ext)) {
			ext = String.valueOf(map.get(LoginKey.ext));
			try {
				String[] exts= ext.split(objSpilt);
				al = Integer.valueOf(exts[0]) == 1;
				type = Integer.valueOf(exts[1]);
				openKey = exts[2];
				pfKey = exts[3];
				yYear = Integer.valueOf(exts[4]) == 1;
				yLevel = Integer.valueOf(exts[5]);
				lv3 = Integer.valueOf(exts[6]);
				if (exts.length >= 8) {
					appCustom = Integer.valueOf(exts[7]);
					//LogUtil.getMain().info(openId +" login by task -> "+ appCustom);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public boolean isPass() {
		StringBuilder sb = new StringBuilder();
		sb.append(ConstantsConfig.getSysCgPartnerEName()).append(objSpilt);// 运营商
		sb.append(openId).append(objSpilt);// 帐号
		sb.append(time).append(objSpilt);// 登录时间
		sb.append(pf).append(objSpilt);// pf
		sb.append(ext).append(objSpilt);// type
		sb.append(ConstantsConfig.getSysCgKeyLogin());
		if (!sign.equals(MD5.compile(sb.toString()))) {
			LogUtil.getMain().error("longin sign err true : " + MD5.compile(sb.toString()));
			return false;
		}
		return true;
	}

	public boolean isLostArgs() {
		return openId == null || sign == null || pf == null;
	}

	public boolean isExpir() {
		return time + 1800 <= TimeProcesser.getUnixTime();
	}

	public void syncContent(Player player) {
		if (player == null || !openId.equals(player.getAccountName())) {
			LogUtil.getMain().error("login err name " + info());
			return;
		}
		player.getDataInfo().setYLevel(yLevel);
		player.getDataInfo().setYYear(yYear);
		if (type == 0) {// 登录type =0 内部登录
			player.getDataInfo().setGmLogin(true);
		}else {
			player.getDataInfo().setGmLogin(false);
		}
		player.getDataInfo().setOpenKey(openKey);
		player.getDataInfo().setPf(pf);
		player.getDataInfo().setPfKey(pfKey);
		player.getDataInfo().setLv3(lv3);
		if (appCustom > 0) {
			player.getDataInfo().intDataSet(ConstantsIntKey.jx,appCustom);
		}
	}

	public String info() {
		StringBuilder sb = new StringBuilder();
		sb.append("openId:").append(openId).append(objSpilt);
		sb.append("openKey:").append(openKey).append(objSpilt);
		sb.append("time:").append(time).append(objSpilt);
		sb.append("al:").append(al).append(objSpilt);
		sb.append("sign:").append(sign).append(objSpilt);
		sb.append("pf:").append(pf).append(objSpilt);
		sb.append("pfKey:").append(pfKey).append(objSpilt);
		sb.append("yYear:").append(yYear).append(objSpilt);
		sb.append("yLevel:").append(yLevel).append(objSpilt);
		sb.append("lv3:").append(lv3).append(objSpilt);
		return sb.toString();
	}

}
